package com.thomas.es;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;

import com.google.gson.JsonObject;

/**   
* @Description: 创建索引
* @author thomas_liu  
* @date 2018年6月13日 下午9:14:01 
* @version V1.0   
*/
public class EsTest {
	
	private static String host ="192.168.198.201";//服务器地址
	private static int port = 9300;//端口
	
	private TransportClient client=null;
	
	/**
	 *获取连接
	 *@return 
	 */
	@SuppressWarnings({"unchecked","resource"})
	@Before
	public void getClient() throws UnknownHostException {
		client = new PreBuiltTransportClient(Settings.EMPTY)
				.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(EsTest.host), EsTest.port));
		System.out.println(client);
	}
	
	/**
	 *关闭连接
	 *@param client 
	 */
	@After
	public void close(){
		if(client != null){
			client.close();
		}
	}
	
	/**
	 *添加索引 
	 */
	@org.junit.Test
	public void testIndex() throws Exception{
		 IndexResponse response =client.prepareIndex("twitter", "tweet", "1")
		            .setSource(XContentFactory.jsonBuilder()
		                    .startObject()
		                    .field("user", "kimchy")
		                    .field("postDate", new Date())
		                    .field("message", "trying out Elasticsearch")
		                .endObject()
		                    )
		            .get();
        System.out.println("索引名称："+response.getIndex());
        System.out.println("类型："+response.getType());
        System.out.println("文档ID："+response.getId()); // 第一次使用是1
        System.out.println("当前实例状态："+response.status());
	}
	
	@org.junit.Test
	public void testIndex2() {
		String json = "{"+
				 "\"user\":\"kimchy\"," +
	                "\"postDate\":\"2013-01-30\"," +
	                "\"message\":\"trying out Elasticsearch\"" +
				"}";
		
		IndexResponse response = client.prepareIndex("weibo", "tweet")
			  .setSource(json, XContentType.JSON)
			  .get();
		
		System.out.println("索引名称:"+response.getIndex());
		System.out.println("类型:"+response.getType());
		System.out.println("json:"+response.getResult());
		System.out.println("文档ID:"+response.getId());
		System.out.println("当前实例状态:"+response.status());
	}
	
	@org.junit.Test
	public void testIndex3() {
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("user", "kimchy");
		json.put("postDate", new Date());
		json.put("message", "trying out Elasticsearch");
		
		IndexResponse response = client.prepareIndex("qq", "tweet")
				.setSource(json)
				.get();
		System.out.println("索引名称:"+response.getIndex());
		System.out.println("类型:"+response.getType());
		System.out.println("文档ID:"+response.getId());
		System.out.println("当前实例状态:"+response.status());
	}
	
	@org.junit.Test
	public void testIndex4()throws Exception{
		JsonObject json = new JsonObject();
		json.addProperty("user", "kimchy");
		json.addProperty("postDate", "1989-11-11");
		json.addProperty("message", "trying out Elasticsearch");
		
		IndexResponse response = client.prepareIndex("qq", "tweet")
				.setSource(json.toString(),XContentType.JSON)
				.get();
		
		System.out.println("索引名称:"+response.getIndex());
		System.out.println("类型:"+response.getType());
		System.out.println("文档ID:"+response.getId());
		System.out.println("当前实例状态:"+response.status());
	}
	
	@org.junit.Test
	public void testGet(){
		GetResponse getResponse=client.prepareGet("twitter", "tweet", "1").get();
		System.out.println(getResponse.getSourceAsString());
	}
	
	
	@org.junit.Test
	public void testUpdate(){
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("user", "锋哥");
		jsonObject.addProperty("postDate", "1989-11-11");
		jsonObject.addProperty("message", "学习ElasticSearch");
		
		UpdateResponse response = client.prepareUpdate("twitter", "tweet", "1").setDoc(jsonObject.toString(), XContentType.JSON).get();
		System.out.println("索引名称:"+response.getIndex());
		System.out.println("类型:"+response.getType());
		System.out.println("文档ID:"+response.getId());
		System.out.println("当前实例状态:"+response.status());
	}
	
	@org.junit.Test
	public void testDelete(){
		DeleteResponse response = client.prepareDelete("twitter", "tweet", "1").get();
		System.out.println("索引名称:"+response.getIndex());
		System.out.println("类型:"+response.getType());
		System.out.println("文档ID:"+response.getId());
		System.out.println("当前实例状态:"+response.status());
	}
	
	
}
